package oa.hleast.xswl.controller.admin.store;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.common.enums.QueryModeEnum;
import oa.hleast.common.result.Result;
import oa.hleast.common.web.util.RequestUtils;
import oa.hleast.xswl.pojo.domain.store.StoreInfo;
import oa.hleast.xswl.pojo.form.store.OpenShopForm;
import oa.hleast.xswl.pojo.form.store.StoreInfoForm;
import oa.hleast.xswl.pojo.form.store.StorePayInfoForm;
import oa.hleast.xswl.pojo.form.store.StoreRegisteredForm;
import oa.hleast.xswl.pojo.vo.store.StorePayInfoVo;
import oa.hleast.xswl.service.ISysUserService;
import oa.hleast.xswl.service.store.StoreInfoService;
import oa.hleast.xswl.service.store.StorePayInfoService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.*;


@Api(tags = "【系统管理】商家信息表")
@RestController("AdminStoreInfoController")
@RequestMapping("/api.admin/v1/storeInfos")
@Slf4j
@AllArgsConstructor
public class StoreInfoController {

    private final StoreInfoService storeInfoService;
    private final StorePayInfoService storePayInfoService;
    private final ISysUserService sysUserService;

    @ApiOperation(value = "列表分页", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryMode", value = "查询模式", paramType = "query", dataTypeClass = QueryModeEnum.class, dataType = "QueryModeEnum"),
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataTypeClass = Long.class, dataType = "Long"),
            @ApiImplicitParam(name = "limit", value = "每页数量", paramType = "query", dataTypeClass = Long.class, dataType = "Long"),
            @ApiImplicitParam(name = "storeName", value = "店铺名称", paramType = "query", dataTypeClass = String.class, dataType = "String"),
            @ApiImplicitParam(name = "source", value = "店铺来源 0未知 1文旅红 2码上游萧山", paramType = "query", dataTypeClass = Long.class, dataType = "Long"),
            @ApiImplicitParam(name = "reviewStatus", value = "审核状态 0待审核 1审核通过 2审核驳回", paramType = "query", dataTypeClass = Long.class, dataType = "Long")
    })
    @GetMapping("/list")
    public Result list(String queryMode, Integer page, Integer limit, String storeName, Integer source, Integer reviewStatus) {
        QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode);
        switch (queryModeEnum) {
            default:

                LambdaQueryWrapper<StoreInfo> queryWrapper = new LambdaQueryWrapper<StoreInfo>()
                        .like(ObjectUtils.isNotEmpty(storeName), StoreInfo::getStoreName, storeName)
                        .eq(ObjectUtils.isNotEmpty(source), StoreInfo::getSource, source)
                        .eq(ObjectUtils.isNotEmpty(reviewStatus), StoreInfo::getReviewStatus, reviewStatus)
                        .orderByDesc(StoreInfo::getGmtModified)
                        .orderByDesc(StoreInfo::getGmtCreate);

                Page<StoreInfo> result = storeInfoService.page(new Page<>(page, limit), queryWrapper);
                return Result.success(result.getRecords(), result.getTotal());
        }
    }


    @ApiOperation(value = "商家注册 代理注册", httpMethod = "POST")
    @PostMapping("/registeredByProxy")
    public Result registeredByProxy(@RequestBody StoreRegisteredForm form) {
        boolean status = storeInfoService.registeredByProxy(form);
        return Result.judge(status);
    }

    @ApiOperation(value = "详情", httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "path", dataTypeClass = Long.class, dataType = "Long")
    @GetMapping("/{id}")
    public Result detail(@PathVariable Long id) {
        return Result.success(storeInfoService.getById(id));
    }

    @ApiOperation(value = "修改", httpMethod = "POST")
    @PostMapping("/update")
    public Result update(
            @RequestBody StoreInfoForm body) {

        boolean status = storeInfoService.updateStoreInfo(body);

        return Result.judge(status);
    }

    @ApiOperation(value = "店铺关店", httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataTypeClass = String.class, dataType = "String")
    @GetMapping("/closeShop")
    public Result closeShop(@RequestParam String id) {
        Boolean status = storeInfoService.closeShop(id);
        return Result.judge(status);
    }

    @ApiOperation(value = "店铺开店", httpMethod = "GET")
    @GetMapping("/openShop")
    public Result openShop(@RequestBody OpenShopForm form) {
        Boolean status = storeInfoService.openShop(form);
        return Result.judge(status);
    }


    @ApiOperation(value = "审核通过", httpMethod = "GET")
    @GetMapping("/pass")
    public Result reviewPass(@RequestParam Long id) {

        Long userId = RequestUtils.getUserId();
        boolean status = storeInfoService.reviewPass(id, userId);

        return Result.judge(status);
    }

    @ApiOperation(value = "审核驳回", httpMethod = "GET")
    @GetMapping("/notPass")
    public Result reviewNotPass(@RequestParam Long id, @RequestParam String remark) {

        Long userId = RequestUtils.getUserId();
        boolean result = storeInfoService.reviewNotPass(userId, id, remark);
        return Result.judge(result);
    }

    @ApiOperation(value = "根据商店id获取商店支付信息", httpMethod = "GET")
    @GetMapping("/getStorePayByStoreId")
    public Result<StorePayInfoVo> getStorePayByStoreId(@RequestParam Long id) {
        StorePayInfoVo result = storePayInfoService.getStorePayVoByStoreId(id);

        return Result.success(result);
    }

    @ApiOperation(value = "修改店铺支付信息", httpMethod = "POST")
    @PostMapping("/updateStorePayInfo")
    public Result<Boolean> updateStorePayInfo(@RequestBody StorePayInfoForm form) {
        boolean result = storePayInfoService.updateStorePayInfo(form);

        return Result.judge(result);
    }

    @ApiOperation(value = "检查是否用户名已被使用", httpMethod = "GET")
    @GetMapping("/checkRepeatUserName")
    public Result<Integer> checkRepeatUserName(@RequestParam String userName) {
        boolean isRepeat = sysUserService.checkRepeatUserName(userName);
        int result = isRepeat ? 1 : 0;

        return Result.success(result);
    }

}
